import http from '../common/request'
import {defineStore} from 'pinia';

export default defineStore('circleStore', {
    state: () => {
        return {
            myCircles: [],
            joinedGroups:[],
            loading: false,
            circleApprovalList:[]
        }
    },
    actions: {
        loadCircle() {
            return new Promise((resolve, reject) => {
                this.loading = true
                http({
                    url: `/circle/user-circle`,
                    method: 'GET',
                }).then((res) => {
                    this.myCircles = res.myCircles;
                    this.joinedGroups = res.joinedGroups;
                    resolve();
                    this.loading = false
                }).catch((res) => {
                    reject(res);
                    this.loading = false
                })
            });
        },
        activeCircle(index) {
            this.activeIndex = index;
        },
        addCircle(circles) {
            this.circles.unshift(circles);
        },
        removeCircle(circleId) {
            this.joinedGroups.forEach((g, index) => {
                if (g.id == circleId) {
                    this.joinedGroups.splice(index, 1);
                    if (this.activeIndex >= this.joinedGroups.length) {
                        this.activeIndex = this.joinedGroups.length - 1;
                    }
                }
            })

        },
        updateCircle(state, circle) {
            this.circles.forEach((g, idx) => {
                if (g.id == circle.id) {
                    // 拷贝属性
                    Object.assign(state.circles[idx], circle);
                }
            })
        },
        clear(state) {
            this.circles = [];
            this.activeCircle = -1;
        },

        openCircle(state, circle) {
            this.currentOpenCircle = circle;
        },


    },
})